package com.tencent.sr.iris.activity.restapi.activity;

import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.client.activity.ActivityBatchDataClient;
import com.tencent.sr.iris.activity.interfaces.request.IrisPaginationRequest;
import com.tencent.sr.iris.activity.interfaces.request.acticity.ActivityTaskRequest;
import com.tencent.sr.iris.activity.interfaces.request.acticity.BatchDataRequest;
import com.tencent.sr.iris.activity.interfaces.response.SearchAfterPaginationResponse;
import com.tencent.sr.iris.activity.interfaces.response.activity.ActivityTierResponse;
import com.tencent.sr.iris.activity.interfaces.response.activity.BatchDataResponse;
import com.tencent.sr.iris.activity.interfaces.response.activity.BatchStatisticsResponse;
import com.tencent.sr.iris.activity.service.activity.BatchDataQueryService;
import com.tencent.sr.iris.activity.service.activity.IrisActivityService;
import com.tencent.sr.iris.activity.service.activity.impl.BatchDataStatisticsService;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Optional;

/**
 * 任务跑批记录
 *
 * @author nixiaolin
 * @date 2023/2/15 18:02
 */
@Api(tags = "任务跑批记录", value = "任务跑批记录相关接口")
@RestController
@RequestMapping(value = {"/api/v1/iris/iris-activity-core-service/activity/batchData"})
public class ActivityBatchDataController implements ActivityBatchDataClient {

    @Resource(name = "irisEsBatchDataQueryServiceImpl")
    private BatchDataQueryService batchDataService;

    @Resource
    private BatchDataStatisticsService statisticsService;

    @Resource
    private IrisActivityService irisActivityService;

    /**
     * 分页查询
     *
     * @author nixiaolin
     * @date 2023/2/15 18:08
     */
    @Override
    @PostMapping("page")
    @ApiOperation(value = "分页查询", httpMethod = "POST")
    public HttpResult<SearchAfterPaginationResponse<BatchDataResponse>> page(
            @Validated @RequestBody IrisPaginationRequest<BatchDataRequest> request) {
        SearchAfterPaginationResponse<BatchDataResponse> response = batchDataService.page(request);
        return HttpResult.succ(response);
    }

    @Override
    @PostMapping("activityStatic")
    @ApiOperation(value = "活动统计", httpMethod = "POST")
    public HttpResult<BatchStatisticsResponse> activityStatic(
            @Validated @RequestBody BatchDataRequest request) {
        BatchStatisticsResponse response = statisticsService.activityStatic(request);
        return HttpResult.succ(response);
    }

    @Override
    @PostMapping("getActivityMaxTier")
    @ApiOperation(value = "获取活动的最大阶数", httpMethod = "POST")
    public HttpResult<ActivityTierResponse> getActivityMaxTier(
            @Validated @RequestBody ActivityTaskRequest request) {
        ActivityDTO activityDTO = irisActivityService.getActivityAllTask(request.getActivityId().getCode());
        Integer maxTier = Optional.ofNullable(activityDTO)
                .map(ActivityDTO::getActivityTaskDTOS).orElse(Collections.emptyList())
                .stream().map(ActivityTaskDTO::getTier).max(Integer::compareTo).orElse(0);
        return HttpResult.succ(new ActivityTierResponse().setMaxTier(maxTier));
    }
}
